home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr31 / rle8_sc.zip / RLE8.C next >
Text File  |  1993-05-14  |  9KB  |  258 lines

  1. /* rle8comp.c                          */
  2. /* RLE compression, uses 8 bit headers */
  3. /* by Shaun Case 1991 Borland C++ 2.0  */
  4. /* Public Domain                       */
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <dir.h>    /* for fnsplit() */
  9.  
  10.  
  11. #include "rle8.h"
  12.  
  13. int main(int argc, char **argv)
  14. {
  15.     register int cur_char;                /* a character                    */
  16.     register unsigned int i;              /* generic index variable         */
  17.     register unsigned short run_len = 0;  /* length of character run so far */
  18.     int run_char;                         /* which char run is of           */
  19.     unsigned int j;                       /* another index variable         */
  20.     unsigned short seq_len=0;             /* length of non-run sequence     */
  21.  
  22.     char scratch_space[256];              /* string scratch space           */
  23.     char seq[MAX_LEN];                    /* buffer for uncompressible data */
  24.  
  25.     char orig_name[MAXFILE+MAXEXT];       /* original filename      */
  26.     char drive[MAXDRIVE],                 /* needed for fnsplit()   */
  27.          dir[MAXDIR],                     /* needed for fnsplit()   */
  28.          filename[MAXFILE],               /* 8 chars, orig name     */
  29.          ext[MAXEXT];                     /* 3 chars, orig ext      */
  30.     char outfile_name[MAXFILE+MAXEXT];    /* output filename        */
  31.  
  32.     unsigned long  orig_total;              /* total number of unencoded bytes          */
  33.     unsigned long  rle_total;               /* total number of encoded bytes            */
  34.     FILE *infile;                           /* file ptr to input file (uncompressed)    */
  35.     FILE *outfile;                          /* file ptr to output file (compressed)     */
  36.     char *infile_name;
  37.  
  38.     fnsplit(argv[1], drive, dir, filename, ext);    /* get filename    */
  39.     strcpy(orig_name, filename);
  40.     strcat(orig_name, ext);
  41.     strcpy(outfile_name, filename);       /* build output filename  */
  42.     strcat(outfile_name, ".r8");
  43.  
  44.     if (argc != 2)
  45.     {
  46.         puts("Usage: RLE8 filename.ext compresses file to filename.r8");
  47.         return 1;
  48.     }
  49.  
  50.     puts("rle8  by Shaun Case 1991  public domain.");
  51.  
  52.     infile_name=argv[1];
  53.  
  54.     if ((infile=fopen(infile_name, "rb")) == NULL)
  55.     {
  56.         strcpy(scratch_space, "Uable to open ");
  57.         strcat(scratch_space, infile_name);
  58.         puts(scratch_space);
  59.         return 1;
  60.     }
  61.     
  62.     if ((outfile=fopen(outfile_name, "wb")) == NULL)
  63.     {
  64.         strcpy(scratch_space, "Uable to open ");
  65.         strcat(scratch_space, outfile_name);
  66.         puts(scratch_space);
  67.         return 1;
  68.     }
  69.  
  70.     for (i = 0; i < 13; i++)            /* write original filename */
  71.         fputc(orig_name[i], outfile);
  72.  
  73.     while (!feof(infile))
  74.     {
  75.         cur_char = fgetc(infile);
  76.  
  77.         if (feof(infile))
  78.             continue;
  79.  
  80.  
  81.         if (seq_len ==0)                /* haven't got a sequence yet   */
  82.         {
  83.             if (run_len == 0)           /* start a new run              */
  84.             {
  85.                 run_char = cur_char;
  86.                 ++run_len;
  87.                 continue;
  88.             }
  89.  
  90.             if (run_char == cur_char)   /* got another char in the run  */
  91.                 if (++run_len == MAX_LEN)
  92.                 {
  93.                     fputc((int)MAX_RUN_HEADER, outfile);
  94.                     fputc((int) run_char, outfile);
  95.  
  96.                     run_len = 0;
  97.                     continue;
  98.                 }
  99.  
  100.                                    /* got a different character     */
  101.                                    /* than the run we were building */
  102.             if (run_len > 2)       /* so write out the run and      */
  103.                                    /* start a new one of the new    */
  104.                                    /* character.                    */
  105.             {
  106.                 fputc((int)(RUN | run_len), outfile);
  107.                 fputc((int)run_char, outfile);
  108.  
  109.                 run_len = 1;
  110.                 run_char   = cur_char;
  111.                 continue;
  112.             }
  113.  
  114.             /* run was only one or two chars, make a seq out of it instead       */
  115.  
  116.             for (j = 0; j < run_len; j++);    /* copy 1 or 2 char run to seq[]   */
  117.             {
  118.                 seq[seq_len] = run_char;
  119.                 ++seq_len;
  120.                 if (seq_len == MAX_LEN)       /* if seq[] is full, write to disk */
  121.                 {
  122.                     fputc((int)MAX_SEQ_HEADER, outfile);
  123.  
  124.                     for (i = 0; i < seq_len; i++)
  125.                         fputc((int)seq[i], outfile);
  126.  
  127.                     seq_len = 0;
  128.                 }
  129.             }
  130.  
  131.             run_len = 0;
  132.  
  133.             seq[seq_len++] = cur_char;
  134.             if (seq_len == MAX_LEN)        /* if seq[] is full, write to disk */
  135.             {
  136.                 fputc((int)MAX_SEQ_HEADER, outfile);
  137.  
  138.                 for (i = 0; i < seq_len; i++)
  139.                     fputc((int)seq[i], outfile);
  140.  
  141.                 seq_len = 0;
  142.             }
  143.         }
  144.         else    /* a sequence exists */
  145.         {
  146.             if (run_len != 0)           /* if a run exists */
  147.             {
  148.                 if (cur_char == run_char )  /* add to run!  Yay.  */
  149.                 {
  150.                     ++run_len;
  151.                     if (run_len == MAX_LEN)  /* if run is full */
  152.                     {
  153.                         /* write sequence that precedes run */
  154.  
  155.                         fputc((int)(SEQ | seq_len), outfile);
  156.  
  157.                         for (i = 0; i < seq_len; i++)
  158.                             fputc((int)seq[i], outfile);
  159.  
  160.  
  161.                         /* write run                        */
  162.  
  163.                         fputc((int)(RUN | run_len), outfile);
  164.                         fputc((int)run_char, outfile);
  165.  
  166.                         /* and start out fresh              */
  167.                         seq_len = run_len = 0;
  168.  
  169.                     }  /* end write full run with existing sequence */
  170.  
  171.                     continue;
  172.  
  173.                 }  /* end add to run for sequence exists */
  174.  
  175.                 /* we couldn't add to the run, and a preceding sequence */
  176.                 /* exists, so write the sequence and the run, and       */
  177.                 /* try starting a new run with the current character.   */
  178.  
  179.                 /* write sequence that precedes run */
  180.  
  181.                 fputc((int)(SEQ | seq_len), outfile);
  182.  
  183.                 for (i = 0; i < seq_len; i++)
  184.                     fputc((int)seq[i], outfile);
  185.  
  186.  
  187.                 /* write run                        */
  188.  
  189.                 fputc((int)(RUN | run_len), outfile);
  190.                 fputc((int)run_char, outfile);
  191.  
  192.                 /* and start a new run w/ cur_char  */
  193.                 seq_len = 0;
  194.                 run_len = 1;
  195.                 run_char = cur_char;
  196.  
  197.                 continue;
  198.  
  199.             }    /* end can't add to existing run, and preceding seq exists */
  200.  
  201.             /* no run exists, but a sequences does.  Try to create a run    */
  202.             /* by looking at cur_char and the last char of the sequence.    */
  203.             /* if that fails, add the char to the sequence.                 */
  204.             /* if the sequence is full, write it to disk.  (Slightly non    */
  205.             /* optimal; we could wait one more char.  A small thing to fix  */
  206.             /* if someone gets the urge...                                  */
  207.  
  208.             if (seq[seq_len - 1] == cur_char)       /* if we can make a run */
  209.             {
  210.                 run_char = cur_char;
  211.                 run_len = 2;
  212.                 --seq_len;
  213.                 continue;
  214.             }
  215.  
  216.             /* couldn't make a run, add char to seq.  Maybe next time       */
  217.             /* around...                                                    */
  218.  
  219.             seq[seq_len++] = cur_char;
  220.  
  221.             if (seq_len == MAX_LEN) /* if the sequence is full, write out   */
  222.             {
  223.                 fputc((int)MAX_SEQ_HEADER, outfile);
  224.  
  225.                 for (i = 0; i < MAX_LEN; i++)
  226.                     fputc((int)seq[i], outfile);
  227.  
  228.                 seq_len = 0;
  229.             }
  230.  
  231.         }  /* end branch on sequence exists */
  232.  
  233.     } /* done with whole file */
  234.  
  235.     /* there may be stuff left that hasn't been written yet; if so, write it */
  236.  
  237.     if (seq_len != 0)  /* write sequence that precedes run */
  238.     {
  239.         fputc((int)(SEQ | seq_len), outfile);
  240.  
  241.         for (i = 0; i < seq_len; i++)
  242.             fputc((int)seq[i], outfile);
  243.     }
  244.  
  245.     if (run_len != 0)  /* write run */
  246.     {
  247.         fputc((int)(RUN | run_len), outfile);
  248.         fputc((int)run_char, outfile);
  249.     }
  250.  
  251.     fclose(infile);
  252.     fclose (outfile);
  253.  
  254.     return 0;
  255.  
  256. }  /* end main() */
  257.  
  258.